/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CrearEmisora.java
 *
 * Created on 24/01/2012, 07:05:57 PM
 */
package GUI;

import Control.EmisoraJpaController;
import Control.exceptions.PreexistingEntityException;
import Entidad.Emisora;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pepe
 */
public class CrearEmisora extends javax.swing.JPanel {

    /** Creates new form CrearEmisora */
    public CrearEmisora() {
        initComponents();
        e = new Emisora();
        controlEmisora = new EmisoraJpaController();
        
        dialTF.addKeyListener(new KeyAdapter(){
            @Override
            public void keyTyped(KeyEvent e) {
                char caracter = e.getKeyChar();
                if(((caracter < '0') || (caracter > '9')) &&
                (caracter != KeyEvent.VK_BACK_SPACE && caracter != '.')){
                    e.consume();
                }
            }
        });
    }
    
    Emisora e;
    EmisoraJpaController controlEmisora;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        nombreTF = new javax.swing.JTextField();
        ciudadTF = new javax.swing.JTextField();
        dialTF = new javax.swing.JTextField();
        crearJB = new javax.swing.JButton();
        statusJL = new javax.swing.JLabel();
        cacelarJB = new javax.swing.JButton();

        jLabel1.setText("Nombre");

        jLabel2.setText("Ciudad");

        jLabel3.setText("Dial");

        crearJB.setText("Crear Emisora");
        crearJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                crearJBActionPerformed(evt);
            }
        });

        cacelarJB.setText("Cancelar");
        cacelarJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cacelarJBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(statusJL, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2)
                                .addComponent(jLabel3))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(ciudadTF, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                                .addComponent(nombreTF, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                                .addComponent(dialTF, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(crearJB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cacelarJB)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cacelarJB, crearJB});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nombreTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ciudadTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(dialTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(statusJL, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(crearJB)
                    .addComponent(cacelarJB))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {ciudadTF, dialTF, nombreTF});

    }// </editor-fold>//GEN-END:initComponents

    

    
private void crearJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crearJBActionPerformed
    statusJL.setText("");
    e = new Emisora();
    
    if(!nombreTF.getText().equals("") || !dialTF.getText().equals("")){
            try {
                e.setNombre(nombreTF.getText());
                e.setDial(Float.parseFloat(dialTF.getText()));
                e.setCiudad(ciudadTF.getText());
                controlEmisora.create(e);
                statusJL.setText("La emisora se ha creado exitosamente");
            } catch (PreexistingEntityException ex) {
                Logger.getLogger(CrearEmisora.class.getName()).log(Level.SEVERE, null, ex);
                statusJL.setText("no se pudo crear la emisora");
            } catch (Exception ex) {
                Logger.getLogger(CrearEmisora.class.getName()).log(Level.SEVERE, null, ex);
                statusJL.setText("no se pudo crear la emisora");
            }
    }else{
        statusJL.setText("Ni el nombre ni el dial pueden estar vacios");
    }
}//GEN-LAST:event_crearJBActionPerformed

private void cacelarJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cacelarJBActionPerformed
    nombreTF.setText("");
    ciudadTF.setText("");
    dialTF.setText("");
}//GEN-LAST:event_cacelarJBActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cacelarJB;
    private javax.swing.JTextField ciudadTF;
    private javax.swing.JButton crearJB;
    private javax.swing.JTextField dialTF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField nombreTF;
    private javax.swing.JLabel statusJL;
    // End of variables declaration//GEN-END:variables
}
